package com.xxl.job.admin.controller.interceptor.weds;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.xxl.job.admin.constant.BizCodeEnum;
import com.xxl.job.admin.controller.annotation.BizLimit;
import com.xxl.job.admin.dto.JsonData;
import com.xxl.job.admin.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @Description 业务接口拦截器
 * @Author lihw
 * @Date 2024/1/16 10:19
 */
@Slf4j
@Component
public class BizInterceptor implements HandlerInterceptor {

    @Value("${uip.common_job.token:weds_default_token}")
    private String bizToken;

    @Value("${uip.common_job.secret:abcde12345!@#$%,}")
    private String aesSecretKey;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (!(handler instanceof HandlerMethod)) {
            return true;    // proceed with the next interceptor
        }

        // if need auth
        boolean needAuth = false;
        HandlerMethod method = (HandlerMethod) handler;
        BizLimit bizLimit = method.getMethodAnnotation(BizLimit.class);
        if (!Objects.isNull(bizLimit)) {
            needAuth = bizLimit.limit();
        }

        if (needAuth) {

            String token = request.getHeader("job_token");
            if (StringUtils.isBlank(token)) {
                log.info("身份校验失败");

                CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.AUTH_FAIL));
                return false;
            }
            try {
                AES aes = SecureUtil.aes(aesSecretKey.getBytes(StandardCharsets.UTF_8));
                String restToken = aes.decryptStr(token);
                if (bizToken.equalsIgnoreCase(restToken)) {
                    return true;
                } else {
                    log.info("身份校验失败");
                    CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.AUTH_FAIL));
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.AUTH_FAIL));
                return false;
            }

        }

        return true;
    }

    public static void main(String[] args) {
        String keyStr = "abcde12345!@#$%,";
        byte[] bytes = keyStr.getBytes();

        AES aes = SecureUtil.aes(bytes);
        String s = aes.encryptBase64("weds_default_token");
        System.out.println(s);

        String abc = "bt+bV2pXTWqa5AmnNq7bZqkMBlbTFQJFs/48zrBjHC4=";

        System.out.println(abc.equals(s));

        String s1 = aes.decryptStr(abc);
        System.out.println(s1);

        // byte[] encode = Base64.getEncoder().encode(abc.getBytes());
        // String s2 = new String(encode);
        // System.out.println(s2);
    }

}
